$(function() {
	var initData = $("#initData").val();
	var ss = initData.split(",");
	initLoad();
});

function initLoad() {
	var cols = ['id', 'IP', '在线时间', '用户信息', '操作'];
	var models = [{
		name: 'id',
		index: 'id',
		fixed: true,
		search: false,
		width: 300
	},
	{
		name: 'host',
		index: 'host',
		search: false,
		width: 60
	},
	{
		name: 'startTime',
		index: 'startTime',
		search: false,
		formatter: function(cv, op, rb) {
			return num2Date(cv) + ' 至 <b>' + num2Date(rb.lastAccessTime) + '</b>';
		}
	},
	{
		name: 'info',
		index: 'id',
		search: false,
		formatter: function(cv, op, rb) {
			var html = '';
			if(rb.uid != null){
			    html += rb.uid + ' ' + rb.nickname+' '+rb.account;
			}
			return html;
		}
	},
	{
		name: 'opt',
		index: 'id',
		search: false,
		width: 50,
		formatter: function(cv, op, rb) {
			var html = '';
			html += '<button onclick="del(\'' + cv + '\')" type="button" class="btn btn-danger  btn-xs">&nbsp;强制下线</button>&nbsp;';
			return html;
		}
	}];

	//加载和搜索
    var datas = {
        "sortorder": "ASC",
        "sortname":""
    };
	loadData("/master/line", "tabdiv", datas, cols, models);
}

function del(id) {
	if (confirm("强制退出吗?")) {
		window.location.href = "/master/line/del?id=" + id;
	}
}

//重载使用
function loadTab(tabDiv, datas) {
	jQuery("#" + tabDiv).jqGrid('setGridParam', {
		datatype: 'json',
		postData: datas
	}).trigger("reloadGrid");
}

//首载使用
function loadData(uriPrefix, tabDiv, datas, cols, models) {
	if (datas.sortorder === undefined) {
		datas.sortorder = 'DESC';
	}
	var hv = $(document).height() - 121;
	$.jgrid.defaults.styleUI = 'Bootstrap';
	$("#" + tabDiv).jqGrid({
		sortorder: datas.sortorder,
		postData: datas,
		url: uriPrefix + '/list',
		datatype: 'json',
		mtype: 'POST',
		rownumbers: true,
		autowidth: true,
		altRows: true,
		shrinkToFit: true,
		rowNum: 20,
		rowList: [20, 50, 100],
		colNames: cols,
		colModel: models,
		pager: '#' + tabDiv + '_page',
		viewrecords: true,
		hidegrid: false,
		multiselect: false,
		height: hv,
		loadComplete: function(a, b, c) {
			$('#' + tabDiv).jqGrid('setLabel', 'rn', 'No.', {
				'text-align': 'center',
				'vertical-align': 'middle',
				"width": "65"
			});
			$("table[id='tabdiv'] tr").each(function(i, d) {
				$(d).children("td:first").width(52)
			});
		}

	});

    //显示序号
	$("#" + tabDiv).jqGrid('setLabel', 'rn', 'No', {'text-align': 'left' },'');

    //窗体改变了，改变表格宽度
    $(window).bind('resize',function() {
        window.location.reload();
    });
}
